package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.*;


public class BibliotecaApp {


    private Scanner input;
    private PrintStream out;
    private Library library;
    private User user;
    private boolean done = false;


    enum Choice {
        LISTBOOKS(1), RESERVEBOOK(2), CHECKMEMBERSHIP(3), SHOWMOVIE(4), QUIT(5);

        private int value;

        private Choice(int value) {
            this.value = value;
        }

    };


    public BibliotecaApp(Scanner input, PrintStream out) {


        this.input = input;
        this.out = out;
    }

    public static void main(String[] args) {

        BibliotecaApp bibliotecaObject = new BibliotecaApp(new Scanner(System.in), System.out);

        bibliotecaObject.printOptions();
        bibliotecaObject.menu();

    }

    public void menu() {

        try {
            library = new Library();

            int choice = 0;
            do {
                choice = Integer.parseInt(input.nextLine());
                checkCondition(choice);

            } while (!done);
        } catch (NumberFormatException e) {
            System.out.println("Please a valid numerical option!!");
        }
    }


    public void checkCondition(int choice) {


        if (choice == Choice.LISTBOOKS.value) {
            System.out.print(library.listBooks());
        } else if (choice == Choice.RESERVEBOOK.value) {

            if (library.aUserLoggedIn() || login(library)) {
                processReservation();
            }

        } else if (choice == Choice.CHECKMEMBERSHIP.value) {
            System.out.println(checkMembership(user));
        } else if (choice == Choice.SHOWMOVIE.value) {

            out.println("Please enter a movie name with no spaces");
            String MovieID = input.nextLine();
            System.out.println(library.listAMovie(MovieID));

        } else if (choice == Choice.QUIT.value) {
            done = true;
        } else {
            out.println("Select a valid option!!");
        }
    }


    public boolean login(Library libraryObject) {
        System.out.println("You need to be logged in to use this option. Please enter a valid username and password.");
        String username = input.nextLine();
        String password = input.nextLine();

        if (libraryObject.isValidUser(username, password)) {
            user = libraryObject.getLoggedInUser(username);
            user.authenticate();
            System.out.println("Login Successful.");
            return true;
        } else {
            System.out.println("Invalid username or password, please enter valid details.");
            return false;
        }
    }


    public void processReservation() {
        out.println("Please enter a book ID");
        String bookID = input.nextLine();
        System.out.println(library.reserveBook(bookID));

    }


    public String checkMembership(User user) {
        if (user != null && user.isLoggedIn()) {
            return user.toString();
        } else {
            return "Please talk to a Librarian. Thank you.";
        }
    }

    public void printOptions() {
        System.out.println("WELCOME TO THE BANGALORE LIBRARY SYSTEM!!");
        System.out.println("1. List Books");
        System.out.println("2. Reserve Book");
        System.out.println("3. Check Membership");
        System.out.println("4. List a movie");
        System.out.println("5. Quit");
    }


}